package implementations;

import interfaces.IGamePanel;
import interfaces.IMainMenu;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import serverController.Server;

;

public class MainMenuImpl extends JFrame implements IMainMenu {

	/**
	 * 
	 */
	private static MainMenuImpl instance;
	private static final long serialVersionUID = 1L;
	private JFrame thisFrame;
	private JTextField battleTextField;
	private JTextField hostnameTextField;
	private JTextField portTextField;

	public static MainMenuImpl getInstance() {
		if (instance == null)
			instance = new MainMenuImpl();
		return instance;

	}

	private MainMenuImpl() {
		thisFrame = this;
		thisFrame = new JFrame();
		thisFrame.setBounds(100, 100, 740, 580);
		thisFrame.setVisible(true);
		thisFrame.setLayout(new BorderLayout(10, 10));

		thisFrame.add(NorthPanel(), BorderLayout.NORTH);
		thisFrame.add(CenterPanel(), BorderLayout.CENTER);
		thisFrame.revalidate();
		thisFrame.addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent arg0) {
				System.exit(0);
				super.windowClosing(arg0);
			}
		});

	}

	private JPanel NorthPanel() {
		JPanel jPanel = new JPanel(new FlowLayout());

		JTextArea nameTextArea1 = new JTextArea("Name: ");
		nameTextArea1.setSize(20, 100);
		nameTextArea1.setEditable(false);
		JTextField nameTextField = new JTextField("ANONIMUS            ");
		nameTextField.setSize(10, 100);
		JSeparator separator = new JSeparator();
		JTextArea battleTextArea = new JTextArea("Battle ID: ");
		battleTextArea.setEditable(false);
		battleTextField = new JTextField("123");
		hostnameTextField = new JTextField("127.0.0.1");
		JTextArea hostnameTextArea = new JTextArea("Ip:");
		JTextArea portTextArea = new JTextArea("Port:");
		portTextField = new JTextField("81");

		// battleTextArea.se
		jPanel.add(nameTextArea1);
		jPanel.add(nameTextField);
		jPanel.add(separator);
		jPanel.add(hostnameTextArea);
		jPanel.add(hostnameTextField);
		jPanel.add(separator);
		jPanel.add(portTextArea);
		jPanel.add(portTextField);
		jPanel.add(separator);

		jPanel.add(battleTextArea);
		jPanel.add(battleTextField);
		jPanel.setVisible(true);

		return jPanel;
	}

	private JPanel CenterPanel() {
		JPanel panel = new JPanel();

		panel.setLayout(new BoxLayout(panel, BoxLayout.PAGE_AXIS));
		JButton createGameButton = new JButton("Create Game");
		createGameButton.setAlignmentX(Component.CENTER_ALIGNMENT);

		createGameButton.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				System.out.println("The server is starting");
				// Random random=new Random(10000);
				// int ID=random.nextInt();
				// MessageBoxImpl messageBox=new MessageBoxImpl("ID: "+ID,
				// thisFrame);
				int id = -1;
				int port = -1;
				// System.out.println("Connecting to the specified game");
				try {
					System.out.println("id?!?!" + battleTextField.getText());
					id = Integer.parseInt(battleTextField.getText());

				} catch (NumberFormatException e) {
					id = -1;
					System.out.println("NAN");
				}
				try {
					port = Integer.parseInt(portTextField.getText());
				} catch (NumberFormatException e) {
					port = -1;
					System.out.println("NAN");
				}

				IGamePanel gamePanel = new GamePanelImpl(thisFrame, "Create",
						id, hostnameTextField.getText(), port);

				// TODO Create a server and return the Battle ID
				super.mouseClicked(arg0);
			}
		});

		JButton connectGameButton = new JButton("Connect to the game");
		connectGameButton.setAlignmentX(Component.CENTER_ALIGNMENT);
		connectGameButton.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {

				int id = -1;
				int port = -1;
				// System.out.println("Connecting to the specified game");
				try {
					System.out.println("id?!?!" + battleTextField.getText());
					id = Integer.parseInt(battleTextField.getText());

				} catch (NumberFormatException e) {
					id = -1;
					System.out.println("NAN");
				}
				port = Integer.parseInt(portTextField.getText());
				// TODO Connect to the battle by ID

				IGamePanel gamePanel = new GamePanelImpl(thisFrame, "Connect",
						id, hostnameTextField.getText(), port);

				// Client.createClient();
				super.mouseClicked(arg0);
			}
		});

		JButton exitGameButton = new JButton("Exit the game");
		exitGameButton.setAlignmentX(Component.CENTER_ALIGNMENT);
		exitGameButton.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				System.out.println("Exiting game");
				System.exit(0);
				super.mouseClicked(arg0);
			}
		});

		panel.add(createGameButton);
		panel.add(connectGameButton);
		panel.add(exitGameButton);
		return panel;
	}

}
